// 模拟数据生成器

import type {
  SalesOverview,
  Contract,
  Project,
  TimeSeriesData,
  MonthlyData,
  Performer,
  TeamMember,
  Milestone
} from '@/types'
import { ContractStatus, ProjectStatus, RiskLevel } from '@/types'

// 随机数生成辅助函数
const random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
const randomFloat = (min: number, max: number) => Math.random() * (max - min) + min
const randomItem = <T>(arr: T[]): T => arr[Math.floor(Math.random() * arr.length)]

// 生成随机日期
function randomDate(start: Date, end: Date): Date {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}

// 生成销售概览数据
export function generateSalesOverview(): SalesOverview {
  const monthlyTrend: MonthlyData[] = []
  const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  
  for (let i = 0; i < 12; i++) {
    monthlyTrend.push({
      month: months[i],
      revenue: random(500000, 2000000),
      contracts: random(10, 50),
      growth: randomFloat(-10, 30)
    })
  }

  const topPerformers: Performer[] = [
    { id: '1', name: '张伟', revenue: random(1000000, 3000000), contracts: random(20, 50) },
    { id: '2', name: '李娜', revenue: random(800000, 2500000), contracts: random(15, 45) },
    { id: '3', name: '王强', revenue: random(700000, 2000000), contracts: random(12, 40) },
    { id: '4', name: '刘芳', revenue: random(600000, 1800000), contracts: random(10, 35) },
    { id: '5', name: '陈明', revenue: random(500000, 1500000), contracts: random(8, 30) }
  ]

  return {
    totalRevenue: random(10000000, 30000000),
    totalContracts: random(100, 500),
    activeProjects: random(20, 100),
    completionRate: randomFloat(70, 95),
    monthlyTrend,
    topPerformers
  }
}

// 生成合同数据
export function generateContracts(count: number = 20): Contract[] {
  const contracts: Contract[] = []
  const clients = ['阿里巴巴', '腾讯', '百度', '京东', '美团', '字节跳动', '华为', '小米', '滴滴', '拼多多']
  const statuses: ContractStatus[] = [
    ContractStatus.PENDING,
    ContractStatus.ACTIVE,
    ContractStatus.COMPLETED,
    ContractStatus.CANCELLED
  ]
  
  for (let i = 0; i < count; i++) {
    const signDate = randomDate(new Date(2023, 0, 1), new Date())
    const expiryDate = new Date(signDate.getTime() + random(180, 730) * 24 * 60 * 60 * 1000)
    
    contracts.push({
      id: `contract-${i + 1}`,
      name: `${randomItem(clients)}合作项目 ${i + 1}`,
      client: randomItem(clients),
      amount: random(100000, 5000000),
      status: randomItem(statuses),
      signDate,
      expiryDate,
      progress: random(0, 100),
      description: `这是一个重要的商业合作项目，涉及多个业务领域。`
    })
  }
  
  return contracts
}

// 生成项目数据
export function generateProjects(count: number = 15): Project[] {
  const projects: Project[] = []
  const statuses: ProjectStatus[] = [
    ProjectStatus.PLANNING,
    ProjectStatus.IN_PROGRESS,
    ProjectStatus.DELAYED,
    ProjectStatus.COMPLETED
  ]
  const riskLevels: RiskLevel[] = [
    RiskLevel.LOW,
    RiskLevel.MEDIUM,
    RiskLevel.HIGH
  ]
  const roles = ['项目经理', '开发工程师', '测试工程师', '产品经理', '设计师']
  const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十']
  
  for (let i = 0; i < count; i++) {
    const startDate = randomDate(new Date(2023, 0, 1), new Date())
    const deadline = new Date(startDate.getTime() + random(30, 180) * 24 * 60 * 60 * 1000)
    
    const team: TeamMember[] = []
    const teamSize = random(3, 8)
    for (let j = 0; j < teamSize; j++) {
      team.push({
        id: `member-${i}-${j}`,
        name: randomItem(names),
        role: randomItem(roles)
      })
    }
    
    const milestones: Milestone[] = []
    const milestoneCount = random(3, 6)
    for (let j = 0; j < milestoneCount; j++) {
      const milestoneDate = new Date(
        startDate.getTime() + 
        (deadline.getTime() - startDate.getTime()) * (j + 1) / milestoneCount
      )
      milestones.push({
        id: `milestone-${i}-${j}`,
        name: `里程碑 ${j + 1}`,
        deadline: milestoneDate,
        completed: Math.random() > 0.5,
        description: `项目的第 ${j + 1} 个重要里程碑`
      })
    }
    
    projects.push({
      id: `project-${i + 1}`,
      name: `项目 ${i + 1}`,
      contractId: `contract-${random(1, 20)}`,
      status: randomItem(statuses),
      progress: random(0, 100),
      startDate,
      deadline,
      team,
      milestones,
      riskLevel: randomItem(riskLevels),
      description: `这是一个重要的项目，需要团队协作完成。`
    })
  }
  
  return projects
}

// 生成时间序列数据
export function generateTimeSeriesData(
  metric: string,
  days: number = 30
): TimeSeriesData[] {
  const data: TimeSeriesData[] = []
  const now = new Date()
  
  for (let i = days - 1; i >= 0; i--) {
    const timestamp = new Date(now.getTime() - i * 24 * 60 * 60 * 1000)
    const baseValue = random(1000, 10000)
    const variation = randomFloat(-500, 500)
    
    data.push({
      timestamp,
      value: Math.max(0, baseValue + variation),
      metric,
      metadata: {
        dayOfWeek: timestamp.getDay(),
        isWeekend: timestamp.getDay() === 0 || timestamp.getDay() === 6
      }
    })
  }
  
  return data
}

// 生成多个指标的时间序列数据
export function generateMultiMetricTimeSeriesData(
  metrics: string[],
  days: number = 30
): TimeSeriesData[] {
  const allData: TimeSeriesData[] = []
  
  metrics.forEach(metric => {
    const data = generateTimeSeriesData(metric, days)
    allData.push(...data)
  })
  
  return allData
}
